﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;

namespace QuanLyKho.Server.BLL
{
    public class Dm_DonViTinhBLL
    {
        public Dm_DonViTinhBLL()
        {
        }

        public Dm_DonViTinh AddNewDmDonViTinh(Dm_DonViTinh objectDmDonViTinh)
        {
            Dm_DonViTinh dmDVTTemple = new Dm_DonViTinh();
            int error = -1;
            try
            {
                dmDVTTemple = objectDmDonViTinh;
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    entities.Dm_DonViTinh.Add(dmDVTTemple);
                    error = entities.SaveChanges();
                    if (error < 0)
                    {
                        dmDVTTemple = null;
                    }
                }
            }
            catch
            {
            }
            return dmDVTTemple;
        }

        public bool UpdateDmDonViTinh(Dm_DonViTinh objectDmDonViTinh)
        {
            Dm_DonViTinh dmDVTTemple = new Dm_DonViTinh();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmDVTTemple = entities.Dm_DonViTinh.SingleOrDefault(c => c.MaDonViTinh == objectDmDonViTinh.MaDonViTinh);
                    dmDVTTemple.TenDonViTinh = objectDmDonViTinh.TenDonViTinh;
                    dmDVTTemple.KichHoat = objectDmDonViTinh.KichHoat;
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public bool DeleteDmDonViTinh(Dm_DonViTinh objectDmDonViTinh)
        {
            Dm_DonViTinh dmDVTTemple = new Dm_DonViTinh();
            int error = -1;
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmDVTTemple = entities.Dm_DonViTinh.SingleOrDefault(c => c.MaDonViTinh == objectDmDonViTinh.MaDonViTinh);
                    entities.Dm_DonViTinh.Remove(dmDVTTemple);
                    error = entities.SaveChanges();
                    if (error >= 0)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        public List<Dm_DonViTinh> ListDmDonViTinh()
        {
            List<Dm_DonViTinh> dmDVTTemple = new List<Dm_DonViTinh>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmDVTTemple = (from c in entities.Dm_DonViTinh select c).ToList();
                }
            }
            catch
            {
            }
            return dmDVTTemple;
        }

        public List<Dm_DonViTinh> FindListDmDonViTinh(string maDVT,string tenDVT)
        {
            List<Dm_DonViTinh> dmDVTTemple = new List<Dm_DonViTinh>();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    if(!string.IsNullOrEmpty(maDVT))
                    {
                        dmDVTTemple = (from c in entities.Dm_DonViTinh
                                       where c.MaDonViTinh.Contains(maDVT)
                                       select c).ToList();
                    }
                    else if (!string.IsNullOrEmpty(tenDVT))
                    {
                        dmDVTTemple = (from c in entities.Dm_DonViTinh
                                       where c.MaDonViTinh.Contains(tenDVT)
                                       select c).ToList();
                    }
                    else
                    {
                        dmDVTTemple = (from c in entities.Dm_DonViTinh
                                       where c.MaDonViTinh.Contains(maDVT)
                                             && c.MaDonViTinh.Contains(tenDVT)
                                       select c).ToList();
                    }
                }
            }
            catch
            {
            }
            return dmDVTTemple;
        }

        public bool FindTrungMaDmDonViTinh(string maDVT)
        {
            Dm_DonViTinh dmDVTTemple = new Dm_DonViTinh();
            try
            {
                using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
                {
                    dmDVTTemple = entities.Dm_DonViTinh.SingleOrDefault(c => c.MaDonViTinh == maDVT);
                    if (dmDVTTemple != null)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }
    }
}
